﻿--HEADER Add cascade delete to [Analytics].[JoinedInvestmentSerie].

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql += N'
ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id))
    + '.' + QUOTENAME(OBJECT_NAME(parent_object_id)) + 
    ' DROP CONSTRAINT ' + QUOTENAME(name) + ';'
FROM sys.foreign_keys
WHERE OBJECT_NAME(parent_object_id) = 'JoinedInvestmentSerie';

EXEC sp_executesql @sql;

GO

ALTER TABLE [Analytics].[JoinedInvestmentSerie]  WITH CHECK ADD  CONSTRAINT [FK__JoinedInvestmentSerie__Investment] FOREIGN KEY([InvestmentId])
REFERENCES [Analytics].[Investment] ([Id])
ON DELETE CASCADE
GO

ALTER TABLE [Analytics].[JoinedInvestmentSerie] CHECK CONSTRAINT [FK__JoinedInvestmentSerie__Investment]
